package cn.com.wxd.service.dispatch.logger;

import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.common.thread.ThreadPoolUtil;

/**
 * 定时任务日志类
 * Created by ComXu on 2017/5/17.
 */
public class CustomTaskLogger {
    private Logger log;
    private String name;
    private String id;

    /**
     * 根据指定的名称来构建日志
     *
     * @param logName
     */
    public CustomTaskLogger(String id, String name) {
        log = LoggerFactory.getLogger(CustomTaskLogger.class);
        this.id = id;
        this.name = name;
    }

    public boolean isDebugEnabled() {
        return log.isDebugEnabled();
    }

    public void debug(String s) {
        insertQueue(s, "DUBUG");
        log.debug(s);
    }

    public boolean isInfoEnabled() {
        return log.isInfoEnabled();
    }

    public void info(String s) {
        insertQueue(s, "INFO");
        log.info(s);
    }

    public boolean isWarnEnabled() {
        return log.isWarnEnabled();
    }

    public void warn(String s) {
        insertQueue(s, "WARN");
        log.warn(s);
    }

    public boolean isErrorEnabled() {
        return log.isErrorEnabled();
    }

    public void error(String s) {
        insertQueue(s, "ERROR");
        log.error(s);
    }

    public void error(String s, Exception e) {
        insertQueue(s + System.getProperty("line.separator")
                + e.getStackTrace().toString(), "ERROR");
        log.error(s, e);
    }

    private void insertQueue(String str, String level) {
        LinkedBlockingQueue<CustomTaskLoggerEntity> queues = CustomTaskLoggerQueues.getQueues();
        CustomTaskLoggerEntity cle = new CustomTaskLoggerEntity();
        cle.setTaskId(id);
        cle.setTaskName(name);
        cle.setMessage(str);
        cle.setLevel(level);
        queues.offer(cle);
        try {
            ThreadPoolUtil.submintThread(new CustomTaskLoggerWriter());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        //启动线程进行刷入到日志
    }
}
